#include <dspSimulator/instruction/subu.h>


bool SUBU::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    return checkStandardCondition(dspMachine);
}

void SUBU::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                             std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);
    // -----------------------------------SUBU (.unit) uint, xuint, ulong--------------------------------------------
    {
        llvm::Function *subu = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "SUBUKind1",
                                                      module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", subu);
        irBuilder->SetInsertPoint(entryBB);

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1Reg = irBuilder->CreateLoad(src1RegPtr);
        auto src1SExt = irBuilder->CreateSExt(src1Reg, irBuilder->getIntNTy(40));

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);
        auto src2SExt = irBuilder->CreateSExt(src2Reg, irBuilder->getIntNTy(40));

        auto res = irBuilder->CreateSub(src1SExt, src2SExt);

        auto dstLoRegPtr = getDstLoRegPtr(module, irBuilder);
        auto dstHiRegPtr = getDstHiRegPtr(module, irBuilder);
        storeLongIntoDst(irBuilder, res, dstLoRegPtr, dstHiRegPtr);
        irBuilder->CreateRetVoid();
    }
    // ----------------------------------- SUBU (.unit) xuint, uint, ulong---------------------------------------------
    {
        llvm::Function *subu = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "SUBUKind2",
                                                      module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", subu);
        irBuilder->SetInsertPoint(entryBB);

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1Reg = irBuilder->CreateLoad(src1RegPtr);
        auto src1SExt = irBuilder->CreateSExt(src1Reg, irBuilder->getIntNTy(40));

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2Reg = irBuilder->CreateLoad(src2RegPtr);
        auto src2SExt = irBuilder->CreateSExt(src2Reg, irBuilder->getIntNTy(40));

        auto res = irBuilder->CreateSub(src1SExt, src2SExt);

        auto dstLoRegPtr = getDstLoRegPtr(module, irBuilder);
        auto dstHiRegPtr = getDstHiRegPtr(module, irBuilder);
        storeLongIntoDst(irBuilder, res, dstLoRegPtr, dstHiRegPtr);
        irBuilder->CreateRetVoid();
    }
}

std::shared_ptr<Instruction> SUBU::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<SUBU> subu;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b0101111110) && existStandardCondition(data)) {
            subu = std::make_shared<SUBU>(dspInstInfo->getAddress(), baseCycle);
            subu->creg_z = extractBits<4>(data, 28);
            subu->dst = extractBits<5>(data, 23).to_ulong();
            subu->src2 = extractBits<5>(data, 18).to_ulong();
            subu->src1 = extractBits<5>(data, 13).to_ulong();
            subu->x = data[12];
            subu->s = data[1];
            subu->p = data[0];
            if (subu->s == 0) {
                subu->funcUnit = FuncUnit::L1;
            } else {
                subu->funcUnit = FuncUnit::L2;
            }
            subu->instFormat = InstFormat::Kind1;
        } else if (checkBits<10>(data, 2, 0b0111111110) && existStandardCondition(data)) {
            subu = std::make_shared<SUBU>(dspInstInfo->getAddress(), baseCycle);
            subu->creg_z = extractBits<4>(data, 28);
            subu->dst = extractBits<5>(data, 23).to_ulong();
            subu->src2 = extractBits<5>(data, 18).to_ulong();
            subu->src1 = extractBits<5>(data, 13).to_ulong();
            subu->x = data[12];
            subu->s = data[1];
            subu->p = data[0];
            if (subu->s == 0) {
                subu->funcUnit = FuncUnit::L1;
            } else {
                subu->funcUnit = FuncUnit::L2;
            }
            subu->instFormat = InstFormat::Kind2;
        }
    }
    return subu;
}

void SUBU::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::string SUBU::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    if (instFormat == InstFormat::Kind1) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    } else if (instFormat == InstFormat::Kind2) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg64Name();
    }
    return res;
}

std::shared_ptr<DspInstruction> SUBU::clone() {
    return std::make_shared<SUBU>(*this);
}
